//
//  ReadLockView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/17.
//

import UIKit
import Lottie

class ReadLockView: UIView {
        
    lazy var readChapterView: ReadChapterView = {
        let view = ReadChapterView()
        return view
    }()
    
    lazy var topView: UIView = {
        return UIView()
    }()
    
    lazy var leftChapterBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("上一章")
        }.disposed(by: rx.disposeBag)
        return btn
    }()

    lazy var rightChapterBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("下一章")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var gestureImage: UIImageView = {
        return UIImageView(image: UIImage(named: "read_lock_gesture"))
    }()
    
    lazy var bomView: UIView = {
        return UIView()
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "ico_clised"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("关闭页面")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "解鎖章節！"
        label.font = .wn_font(name: .bold, size: 18)
        label.textAlignment = .center
        return label
    }()

    lazy var oneBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("解锁单章")
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(scaleWidth(24))
        return btn
    }()
    
    lazy var userLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textAlignment = .center
        return label
    }()
    
    lazy var agreeBtn: UIButton = {
        var btn = UIButton(type: .custom)
        btn.setTitleColor(.black, for: .normal)
        btn.setTitle("自動解鎖下一章", for: .normal)
        btn.setImage(UIImage(named: "setting_feedback_chose_gray"), for: .normal)
        btn.setTitleColor(.black, for: .selected)
        btn.setTitle("自動解鎖下一章", for: .selected)
        btn.setImage(UIImage(named: "library_book_sel"), for: .selected)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.rx.tap.bind { [weak self] in
            btn.isSelected = !btn.isSelected
            saveAutoOpen(btn.isSelected)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var lineView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "icon_imaginary_line"))
        return view
    }()
    
    lazy var storeView: ReadLockStoreView = {
        let view = ReadLockStoreView()
        return view
    }()
    
    lazy var animationView: LottieAnimationView = {
        var view = LottieAnimationView()
        let animation = LottieAnimation.named("lock")
        view.animation = animation
        view.animationSpeed = 1.0
        view.shouldRasterizeWhenIdle = true
        view.loopMode = .loop
        view.isHidden = true
        return view
    }()
    
    var bookDetailModel: BookDetailModel?
    
    var chapterModel: ChapterDetailModel? {
        didSet {
            if let c = chapterModel {
                darkModeConfig()
                animationView.stop()
                if bookDetailModel?.feeType == 1 {
                    readChapterView.chapterLabel.text = chapterModel?.chapterName
                    agreeBtn.isHidden = true
                    leftChapterBtn.isHidden = true
                    rightChapterBtn.isHidden = true
                    gestureImage.isHidden = true
                    animationView.isHidden = false
                    animationView.play()
                    titleLabel.text = "解鎖全部章節！"
                    oneBtn.setTitle("花費\(bookDetailModel?.price ?? 0)金幣閱讀全本", for: .normal)
                }else if bookDetailModel?.feeType == 2 {
                    readChapterView.content = c.pageArr?[0].showContent
                    agreeBtn.isHidden = false
                    leftChapterBtn.isHidden = false
                    rightChapterBtn.isHidden = false
                    gestureImage.isHidden = false
                    animationView.isHidden = true
                    titleLabel.text = "解鎖章節！"
                    oneBtn.setTitle("花費\(c.price)金幣閱讀\(c.chapterName)", for: .normal)
                }else {
                    
                }
            }
        }
    }
    
    var btnClosure: StringClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        
        addSubview(readChapterView)
        addSubview(topView)
        addSubview(leftChapterBtn)
        addSubview(rightChapterBtn)
        addSubview(gestureImage)
        addSubview(bomView)
        addSubview(closeBtn)
        addSubview(titleLabel)
        addSubview(oneBtn)
        addSubview(agreeBtn)
        addSubview(lineView)
        addSubview(userLabel)
        addSubview(storeView)
        addSubview(animationView)
        readChapterView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo(ScreenH-TabBarTotalHeight-itemHeight-scaleWidth(48)-147)
        }
        leftChapterBtn.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight+scaleWidth(50))
            make.left.equalTo(0)
            make.width.equalTo(ScreenW*0.5)
            make.bottom.equalTo(topView.snp.bottom)
        }
        rightChapterBtn.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight+scaleWidth(50))
            make.left.equalTo(leftChapterBtn.snp.right)
            make.right.equalTo(0)
            make.bottom.equalTo(topView.snp.bottom)
        }
        gestureImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(topView.snp.bottom).offset(-50)
            make.height.width.equalTo(50)
        }
        bomView.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom)
            make.left.bottom.right.equalTo(0)
        }
        closeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bomView.snp.top).offset(0)
            make.right.equalTo(-6)
            make.width.height.equalTo(32)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(topView.snp.bottom).offset(6)
        }
        oneBtn.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(48)
            make.right.equalToSuperview().offset(-48)
            make.height.equalTo(scaleWidth(48))
        }
        agreeBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20-TabBarMargin)
            make.height.equalTo(20)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.bottom.equalTo(agreeBtn.snp.top).offset(-14)
            make.right.equalTo(-20)
            make.height.equalTo(1.5)
        }
        userLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(lineView.snp.top).offset(-14)
        }
        storeView.snp.makeConstraints { (make) in
            make.top.equalTo(oneBtn.snp.bottom).offset(30)
            make.left.right.equalTo(0)
            make.height.equalTo(itemHeight)
        }
        animationView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(bomView.snp.top).offset(-50)
            make.width.equalTo(200)
            make.height.equalTo(200)
        }
        agreeBtn.layoutButton(style: .left, imageTitleSpace: 4)
    }
    
    func darkModeConfig() {
        bomView.backgroundColor = isDark() ? .init(named: "black_121212") : .white
        topView.backgroundColor = WNColor.Fade().lockTop2Bottom(.init(width: ScreenW, height: ScreenH-TabBarTotalHeight-itemHeight-scaleWidth(48)-147))
        titleLabel.textColor = isDark() ? .init(named: "gray_777777") : .black
        userLabel.textColor = isDark() ? .init(named: "gray_777777") : .black

        gestureImage.image = UIImage(named: isDark() ? "read_lock_gesture_dark" : "read_lock_gesture")
        oneBtn.backgroundColor = .init(named: isDark() ? "red_A15F73" : "red_FE95B4")
        oneBtn.setTitleColor(isDark() ? .init(named: "red_D0AFB9") : .white, for: .normal)
        
        agreeBtn.setTitleColor(isDark() ? .init(named: "gray_777777") : .black, for: .normal)
        agreeBtn.setTitleColor(isDark() ? .init(named: "gray_777777") : .black, for: .selected)
        
        userLabel.attributedText = "金幣餘額 \(WNSingleData.shared.userGold)".wn_numberChange(.init(named: isDark() ? "red_A15F73" : "red_FE95B4")!, .wn_font(name: .bold, size: 18))
        
        agreeBtn.isSelected = getAutoOpen()
        
        readChapterView.darkModeConfig()
        storeView.darkModeConfig()
        layoutIfNeeded()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
